package net.child.chess.pieces;

import java.util.List;
import net.child.chess.utils.ChessDef;
import net.child.chess.utils.Position;

//Interface to Rock/Bishop/Queen 
public abstract class PowerfullPiece extends Piece 
{
	public PowerfullPiece(ChessDef.Color color, Type type, PieceState state, int imageId)
	{
		super(color, type, state, imageId);
	}
	
	protected void rookAttacks(List<Integer> squares, IMoves iMoves)
	{
		Position pos = new Position(position());
		while(pos.incX())
		{
			if(addAttackIfValid_(squares, pos.position(), iMoves) == false)
			{
				break;
			}
		}
		
		pos.position(position());
		while(pos.decX())
		{
			if(addAttackIfValid_(squares, pos.position(), iMoves) == false)
			{
				break;
			}
		}
		
		pos.position(position());
		while(pos.incY())
		{
			if(addAttackIfValid_(squares, pos.position(), iMoves) == false)
			{
				break;
			}
		}
		
		pos.position(position());
		while(pos.decY())
		{
			if(addAttackIfValid_(squares, pos.position(), iMoves) == false)
			{
				break;
			}
		}
	}
	
	protected void bishopAttacks(List<Integer> squares, IMoves iMoves)
	{
		Position pos = new Position(position());
		while(pos.incX() && pos.incY())
		{
			if(addAttackIfValid_(squares, pos.position(), iMoves) == false)
			{
				break;
			}
		}
		
		pos.position(position());
		while(pos.incX() && pos.decY())
		{
			if(addAttackIfValid_(squares, pos.position(), iMoves) == false)
			{
				break;
			}
		}
		
		pos.position(position());
		while(pos.decX() && pos.incY())
		{
			if(addAttackIfValid_(squares, pos.position(), iMoves) == false)
			{
				break;
			}
		}
		
		
		pos.position(position());
		while(pos.decX() && pos.decY())
		{
			if(addAttackIfValid_(squares, pos.position(), iMoves) == false)
			{
				break;
			}
		}
	}
	
	//	Check whether it is valid to attack specific square.
	//	return false if we should stop walking to find more squares to attack. 
	private boolean addAttackIfValid_(List<Integer> squares, int to, IMoves iMoves)
	{
		if(iMoves.isEmptySquare(to))
		{
			addAttackIfValid(squares, to, iMoves);
			return true;
		}
		else
		{
			addAttackIfValid(squares, to, iMoves);
			return false;
		}
	}
}

